import { EventType } from "../GameSpecial/GameEventType";
import { GlobalEnum } from "../GameSpecial/GlobalEnum";
import EventManager from "./EventManager";

/**微信小游戏阿拉丁数据统计管理器*/
export default class ALDManager {
    private apiName: string = 'wx';
    private api: any;
    protected openId: string;
    public init(openId: string) {
        this.openId = openId;
        this.api = window[this.apiName];
        if (!!this.api.aldStage) {
            this.onEvents();
            this.api.aldSendOpenid(this.openId);
        }
    }
    protected onEvents() {
        EventManager.on(EventType.Common.ALDEvent.levelStart, this.onStart, this);
        EventManager.on(EventType.Common.ALDEvent.levelWin, this.onLevelWin, this);
        EventManager.on(EventType.Common.ALDEvent.levelLose, this.onLevelLose, this);
        EventManager.on(EventType.Common.ALDEvent.sendEvent, this.sendEvent, this);
        EventManager.on(EventType.Common.ALDEvent.clickGame, this.clickGame, this);
        EventManager.on(EventType.Common.ALDEvent.enterGameFlow, this.enterGameFlow, this);
        EventManager.on(EventType.Common.ALDEvent.enterRecommendFlow, this.enterRecommendFlow, this);
        EventManager.on(EventType.Common.ALDEvent.jumpGame, this.jumpGame, this);
        EventManager.on(EventType.Common.ALDEvent.touchBanner, this.touchBanner, this);
    }

    /**
     * 关卡开始
     * @param lv    关卡编号
     */
    private onStart(lv: number) {
        let data = {
            stageId: lv.toString(),         //关卡id
            stageName: "第" + lv + "关",    //关卡名称
            userId: this.openId,            //玩家id
        };
        this.api.aldStage.onStart(data);
    }
    /**
     * 关卡中事件
     * @param data 
     * @param data.stageId      关卡id
     * @param data.stageName    关卡名称
     * @param data.userId       用户id
     * @param data.event        事件名称
     * @param data.params       事件参数
     * @param data.params.itemName  道具名称
     * @param data.params.itemCount 道具数量
     * @param data.params.desc      描述
     * @param data.params.itemMoney 道具价格 
     */
    private onRunning(data) {
        this.api.aldStage.onRunning(data);
    }

    /**
     * 关卡胜利
     * @param lv 关卡编号
     */
    private onLevelWin(lv: number) {
        let data = this.convertData(lv);
        data.event = "complete";
        data.params = {
            desc: "关卡胜利",
        };
        this.api.aldStage.onEnd(data);
    }
    /**
     * 关卡失败
     * @param lv 关卡编号
     */
    private onLevelLose(lv: number) {
        let data = this.convertData(lv);
        data.event = "fail";
        data.params = {
            desc: "关卡失败",
        };
        this.api.aldStage.onEnd(data);
    }

    /**
     * 通过关卡编号生成阿拉丁统计需要的基础数据
     * @param lv 
     */
    private convertData(lv: number): any {
        return {
            stageId: lv.toString(),         //关卡id
            stageName: "第" + lv + "关",    //关卡名称
            userId: this.openId,            //玩家id
        };
    }

    /**
     * 上报自定义事件
     * @param data 
     * @param data.eventName    事件名称
     * @param data.data         事件内容
     */
    protected sendEvent(data: { eventName: string, data: any }) {
        this.api.aldSendEvent(data.eventName, data.data);
    }

    //游戏流程节点
    protected enterGameFlow(step) {
        let name = this.getGameFlowName(step);
        this.sendEvent({
            eventName: "进入" + name,
            data: {},
        });
    }
    protected getGameFlowName(step) {
        switch (step) {
            case GlobalEnum.Common.GameFlowStep.award: return "奖励页";
            case GlobalEnum.Common.GameFlowStep.initScene: return "加载页";
            case GlobalEnum.Common.GameFlowStep.level: return "关卡";
            case GlobalEnum.Common.GameFlowStep.lobby: return "首页";
            case GlobalEnum.Common.GameFlowStep.lose: return "失败页";
            case GlobalEnum.Common.GameFlowStep.recommendDialog: return "首页弹窗";
            case GlobalEnum.Common.GameFlowStep.win: return "成功页";
            default: return step;
        }
    }

    //导流页
    protected enterRecommendFlow(gameFlow, recommendFlow) {
        let n1 = this.getGameFlowName(gameFlow);
        let n2 = this.getRecommendFlowName(recommendFlow);
        this.sendEvent({
            eventName: n1 + "进入" + n2,
            data: {}
        });
    }
    protected getRecommendFlowName(step) {
        switch (step) {
            case GlobalEnum.Common.RecommendFlowStep.bigPage: return "大全导";
            case GlobalEnum.Common.RecommendFlowStep.insertAd: return "插屏广告";
            case GlobalEnum.Common.RecommendFlowStep.littlePage: return "小全导";
            case GlobalEnum.Common.RecommendFlowStep.middlePage: return "中全导";
            case GlobalEnum.Common.RecommendFlowStep.touchBannerUI: return "砸蛋";
            case GlobalEnum.Common.RecommendFlowStep.video: return "视频";
            default: return step;
        }
    }

    //广告位跳转
    protected clickGame(gameFlow, recommendFlow) {
        let n1 = this.getGameFlowName(gameFlow);
        let n2 = this.getRecommendFlowName(recommendFlow);
        this.sendEvent({
            eventName: n1 + "-" + n2 + "点击广告位",
            data: {}
        });
    }
    protected jumpGame(gameFlow, recommendFlow) {
        let n1 = this.getGameFlowName(gameFlow);
        let n2 = this.getRecommendFlowName(recommendFlow);
        this.sendEvent({
            eventName: n1 + "-" + n2 + "跳转广告位",
            data: {}
        });
    }

    //banner
    protected touchBanner(gameFlow, recommendFlow?) {
        let n1 = this.getGameFlowName(gameFlow);
        let n2 = !!recommendFlow ? this.getRecommendFlowName(recommendFlow) : "";
        this.sendEvent({
            eventName: n1 + "-" + n2 + "点击banner",
            data: {}
        });
    }

}

